package com.mc.web.controller.document;

import com.alibaba.fastjson.JSONObject;
import com.mc.common.core.controller.BaseController;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.UUID;

@RestController
@RequestMapping("/document")
@Api("图片上传")
public class DocumentController extends BaseController {
    @PostMapping("/uploadMulterFile")
    @ResponseBody
    public Object uploadMulterFile (@RequestParam MultipartFile file) {

        String name =file.getOriginalFilename(); //获取图片的名称
        String uuid  = UUID.randomUUID().toString().replaceAll("-","");
        FileOutputStream picOutput = null;//设置存储路径
        try {
            picOutput = new FileOutputStream("/home/industry/pictures/"+uuid+"-"+name);
            //picOutput = new FileOutputStream("E:\\mc\\photo\\"+uuid+"-"+name);
            picOutput.write(file.getBytes());//获取字节流直接写入到磁盘内

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }catch (IOException e){
            e.printStackTrace();
        } finally{
            try {
                picOutput.close();//关闭字节流
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        JSONObject result = new JSONObject();
        result.put("code", "200");
        result.put("path", "/home/industry/pictures/"+uuid+"-"+name);
        //result.put("path", "E:\\mc\\photo\\"+uuid+"-"+name);
        result.put("fileName", uuid+"-"+name);
        return result;
    }
}
